package com.fduss.sota.service.impl;

import com.fduss.sota.dao.RiskCheckPlanDao;
import com.fduss.sota.dao.RiskCheckResultSheetDao;
import com.fduss.sota.domain.Company;
import com.fduss.sota.domain.RiskCheckPlan;
import com.fduss.sota.domain.RiskCheckResultSheet;
import com.fduss.sota.domain.RiskCheckTemplate;
import com.fduss.sota.service.RiskCheckPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.DateTimeException;
import java.sql.Date;
import java.util.List;

/**
 * @Author duocai wu
 * @Date 2017/6/19
 * @Time 17:24
 */
@Service
@Transactional
public class RiskCheckPlanServiceImpl implements RiskCheckPlanService {

    @Autowired
    private RiskCheckPlanDao riskCheckPlanDao;
    @Autowired
    private RiskCheckResultSheetDao riskCheckResultSheetDao;

    @Override
    public RiskCheckPlan createRiskCheckPlan(RiskCheckTemplate template, List<Company> companies, Date endDate) {
       checkRiskCheckTemplate(template);
       Assert.notEmpty(companies, "there should be at least one company.");
       Date startDate = new Date(System.currentTimeMillis());
       if (startDate.compareTo(endDate) >= 0) {
           throw new DateTimeException("The end date must be later then today");
       }
       RiskCheckPlan riskCheckPlan = new RiskCheckPlan(startDate, endDate, template, companies);
       return riskCheckPlanDao.save(riskCheckPlan);
    }

    @Override
    public RiskCheckPlan get(Long id) {
        return riskCheckPlanDao.getOne(id);
    }

    @Override
    public List<RiskCheckPlan> getAll() {
        return riskCheckPlanDao.findAll();
    }

    @Override
    public void delete(long id) {
        riskCheckPlanDao.delete(id);
    }

    @Override
    public void delete(RiskCheckPlan riskCheckPlan) {
        riskCheckPlanDao.delete(riskCheckPlan);
    }

    @Override
    public void deleteAll() {
        riskCheckPlanDao.deleteAll();
    }

    @Override
    public void updateRiskCheckResultSheet(RiskCheckResultSheet riskCheckResultSheet) {
        if (riskCheckResultSheetDao.getOne(riskCheckResultSheet.getId()) == null)
            return;
        riskCheckResultSheetDao.save(riskCheckResultSheet);
    }

    @Override
    public List<RiskCheckResultSheet> getRiskCheckResultSheets(Company company) {
        return riskCheckResultSheetDao.getRiskCheckResultSheetsByCompany(company);
    }

    private void checkRiskCheckTemplate(RiskCheckTemplate riskCheckTemplate) {
        Assert.notNull(riskCheckTemplate.getName(), "the name should not be null");
        Assert.notNull(riskCheckTemplate.getDescription(), "the description should not be null");
        Assert.notNull(riskCheckTemplate.getRiskCheckTemplateItems(), "the items should not be null");
        Assert.notEmpty(riskCheckTemplate.getRiskCheckTemplateItems(), "the items should not be empty");
    }
}
